package leetCode.firstWeek;

public class Code_19 {
  public ListNode removeNthFromEnd(ListNode head, int n) {
    int size = 0;
    ListNode p = head, res = head, tmp = res;
    while (p != null) { // 得到size
      size++;
      p = p.next;
    }
    if (size - n <= 0) {
      return res.next;
    }
    for (int i = 0; i <= size - n; i++) {
      if (i == size - n - 1 && res.next != null) {
        res.next = res.next.next;
        break;
      }
      res = res.next;
    }
    return tmp;
  }
}
